package com.bgybmi.controller.meet;

import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bgybmi.common.ResEnum;
import com.bgybmi.config.ConfigEnum;
import com.bgybmi.controller.BaseController;
import com.bgybmi.controller.BxEnum;
import com.bgybmi.entity.BxCreate;
import com.bgybmi.entity.BxDevice;
import com.bgybmi.entity.BxImgCreate;
import com.bgybmi.entity.CommonStatic;
import com.bgybmi.entity.JbDevice;
import com.bgybmi.entity.JbPhone;
import com.bgybmi.entity.JbRoom;
import com.bgybmi.entity.MeetingCreate;
import com.bgybmi.entity.MeetCreate;
import com.bgybmi.entity.Users;
import com.bgybmi.sender.Sender;
import com.bgybmi.sender.WeChatFactory;
import com.bgybmi.service.MeetService;
import com.bgybmi.thread.SendMsg;
import com.jfinal.kit.PropKit;
import com.jfinal.qyweixin.sdk.api.ApiResult;
import com.jfinal.qyweixin.sdk.api.LoginApi;
import com.jfinal.qyweixin.sdk.api.OAuthApi;
import com.jfinal.qyweixin.sdk.api.SendMessageApi;
import com.jfinal.qyweixin.sdk.msg.send.QiYeTextMsg;
import com.jfinal.qyweixin.sdk.msg.send.Text;
import com.mysql.cj.fabric.xmlrpc.base.Array;

//@Before(OAuth2Intrerceptor.class)

public class MeetController extends BaseController {
	
	public final static String baseurl="/getOathUrl?subUrl="+BxEnum.meetinfo;
	public static final String agentId=PropKit.get(ConfigEnum.HY.getAgentId());
	
	/**
	 * 1.tab row 1.1get Userfrom Cookie 1.2verify jsticket
	 */
	public void index() {
		JsVerify(agentId);
		jspAttr();
		List<JbRoom> li = MeetService.me.getAllBase();
		setAttr("room", li);
		setAttr("myFocusList", li);
		// 关注的记录
		// renderJson();
		renderJsp("index.jsp");
	}

	/**
	 * 1.2 nav view route
	 * 
	 * @render render jsp 4个
	 */
	public void meetroomnav() {
		// Attr set
		JsVerify(agentId);
		jspAttr();
		// 我关注的信息
		List<JbRoom> li = MeetService.me.getAllBase();
		renderJsp("index.jsp");
	}

	public void ordernav() {
		// Attr set
		JsVerify(agentId);
		jspAttr();
		String userid = getSessionAttr("loginuser");
		setAttr("loginuser", userid);
		renderJsp("order.jsp");
	}

	public void rcnav() {
		// Attr set
		JsVerify(agentId);
		jspAttr();
		// 获取 “GMT+08:00”对应的时区
		String userid = getSessionAttr("loginuser");
		List<MeetCreate> li = MeetService.me.getList(userid);
		setAttr("rcli", li);
		renderJsp("rc.jsp");
	}

	public void meetselfnav() {
		// Attr set
		JsVerify(agentId);
		jspAttr();
		String userid = getSessionAttr("loginuser");
		List<Users> umsg = MeetService.me.findByUsername(userid);
		setAttr("umsg", umsg);
		renderJsp("myself.jsp");
	}

	public void listDevice() {
		renderJson(JbDevice.me.listAllDevice());
	}

	public void listDeviceByRoom() {
		String roomname = getPara("roomname");
		List<BxDevice> lab = MeetService.me.listAllByParam("roomname", roomname);
		renderJson(lab);
	}

	public void listDeviceByRoomXh() {
		String roomxh = getPara("roomxh");
		List<BxDevice> lab = MeetService.me.listAllByParam("roomxh", roomxh);
		renderJson(lab);
	}

	/**
	 * 1.3 route2 forword
	 */
	// detail
	public void meetrc() {
		// 加入会议的情况
		JsVerify(agentId);
		jspAttr();
		List<MeetCreate> alt = MeetService.me.getAllMeetTillNow();
		setAttr("meetli", alt);
		renderJsp("meetrc.jsp");
	}

	public void meetinfo() {
		JsVerify(agentId);
		jspAttr();
		renderJsp("meetinfo.jsp");
	}

	/**
	 * 1.4 Json set
	 * 
	 * @action user btn or other request
	 */
	public void meetinfoQuery() {
		String xh = getPara("xh");
		Map ma = MeetService.me.meetinfoQuery(xh);
		// Map ma=MeetService.me.getMeetByXh(xh);
		renderJson(ma);
		// renderJson(every);
	}

	// 会议室页面的预定跳转
	public void meetingorder() {
		String roomname = getPara("roomname");
		String date = getPara("date");
		renderJson();
	}

	/**
	 * @description 前端不喜欢通过两层获取，那就直接给一层 使用renderJson(data);方式
	 */
	// 1.31 MEET attion
	public void myfocus() {
		List<JbRoom> li = MeetService.me.getAllBase();
		// setAttr("roomli",ab);
		// 关注的记录
		renderJson(li);
	}

	public void mycreate() {
		// create记录
		String userid = getSessionAttr("loginuser");
		System.out.println("create部分的" + userid);
		List<JbRoom> li = MeetService.me.getAllCreate(userid);
		// setAttr("roomli",ac);
		// MeetingCreate me = new MeetingCreate();
		renderJson(li);
	}

	
	
	// 1，32 Order action 点入某个页面的去做预定
	public void order() {
		String date = getPara("date");
		// TODO save操作
		String userid = getSessionAttr("loginuser");
		List<JbRoom> al = MeetService.me.getAllList(userid);
		// setAttr("meetingli",al);
		renderJson(al);
	}

	public void getRoomMsg() {
		String roomname = getPara("roomname");
		List<JbRoom> al = MeetService.me.listRoom(roomname);
		renderJson(al);
	}

	public void findByMeetXh(){
		String meetxh=getPara("meetxh");
		renderJson(MeetService.me.findByMeetXh(meetxh));
	}
	/**
	 * @data_eg topic=测试 room=20层咖啡厅 starttime=2017-11-25T09:00
	 *          finishtime=2017-11-25T12:00 zcr=hecore ydr=hecore chr=hecore
	 *          prechr=20 bz=哈哈 isprivate=on isremind=30
	 */
	public void submit() {
		String room = getPara("room");
		String topic = getPara("topic");
		String zcr = getPara("zcr");// ??
		String starttime = getPara("starttime");
		String finishtime = getPara("finishtime");
		// finishtime finishtime
		String chr = getPara("chr");
		String prechr = getPara("prechr");
		String bz = getPara("bz");
		String ydr = getPara("ydr");// 空
		String device = getPara("device");
		String isprivate = getPara("isprivate");
		String isremind = getPara("isremind");
		String isdrink=getPara("isdrink");
		
		String url = "http://" + getRequest().getServerName() + baseurl;
		synchronized (room) {//对象锁
			boolean bsearch=MeetService.me.search(room,starttime,finishtime);
			if(bsearch==true){
				setAttr("res",ResEnum.FK.getRes());
			}else{
				boolean bl=false;
				if("0".equals(isdrink)){
					 bl= MeetService.me.submit(room, topic, zcr, starttime, finishtime, chr, prechr, bz, ydr, device,
							isprivate, isremind,isdrink,url);
				}else{
					//如何来使用上面生成的id呢?
					String meis=getPara("drink[meis]");
					String naik=getPara("drink[naik]");
					String kaib=getPara("drink[kaib]");
					String nat=getPara("drink[nat]");
					String xiangc=getPara("drink[xiangc]");
					String zheng=getPara("drink[zheng]");
					String jiaot=getPara("drink[jiaot]");
					String mok=getPara("drink[mok]");
					String guoz=getPara("drink[guoz]");
					bl= MeetService.me.submit(room, topic, zcr, starttime, finishtime, chr, prechr, bz, ydr, device,
								isprivate, isremind,isdrink,url,meis,naik,kaib,nat,xiangc,zheng,jiaot,mok,guoz);
				}

				//if--else

				String drink=getPara("drink");
				System.out.println(drink);			
				setAttr("res",bl);
			}	
		}
		System.out.println(getAttr("res")+"返回前端");
		renderJson();
	}

	public void orderByRoom() {
		String room = getPara("roomname");
		List<MeetCreate> abr = MeetService.me.getAllByRoom(room);
		// List<Map> results = new ArrayList<Map>();
		System.out.println("获取待" + abr.size());
		renderJson(abr);
	}

	// 1.4function

	public void create() {
		String userid = getSessionAttr("loginuser");
		String roomname = getPara("roomname");
		String bz = getPara("bz");
		boolean bs = MeetService.me.create(roomname, bz, userid);
		setAttr("res", bs);
		renderJson();
	}

	public void edit() {
		boolean editRes = false;
		String xh = getPara("xh");
		String roomname = getPara("roomname");
		String bz = getPara("bz");
		// Mee
		editRes = MeetService.me.edit(xh, roomname, bz);
		setAttr("res", editRes);
		renderJson();
	}

	public void delete() {

		String xh = getPara("xh");
		boolean deleteRes=MeetService.me.delete(xh);

		setAttr("res", deleteRes);
		renderJson();
	}

	public void update() {// meetEdit
		boolean editRes = false;
		String xh = getPara("xh");
		String room = getPara("room");
		String topic = getPara("topic");
		String zcr = getPara("zcr");// ??
		String starttime = getPara("starttime");
		String finishtime = getPara("finishtime");
		// finishtime finishtime
		String chr = getPara("chr");
		String prechr = getPara("prechr");
		String bz = getPara("bz");
		String ydr = getPara("ydr");// 空
		String device = getPara("device");
		String isprivate = getPara("isprivate");
		String isremind = getPara("isremind");
		String isdrink=getPara("isdrink");
		// Mee
		//锁定room 防止并发
		synchronized (room) {
			boolean bsearch=MeetService.me.search(xh,room,starttime,finishtime);
			if(bsearch==true){
				setAttr("res",ResEnum.FK.getRes());
			}else{
				String url = "http://" + getRequest().getServerName() + baseurl;
				if("0".equals("drink")){
					editRes=MeetService.me.update(xh,room,topic,starttime,finishtime,zcr,chr,prechr,bz,ydr,device,isprivate,isremind,url);
				}else{
					String meis=getPara("drink[meis]");
					String naik=getPara("drink[naik]");
					String kaib=getPara("drink[kaib]");
					String nat=getPara("drink[nat]");
					String xiangc=getPara("drink[xiangc]");
					String zheng=getPara("drink[zheng]");
					String jiaot=getPara("drink[jiaot]");
					String mok=getPara("drink[mok]");
					String guoz=getPara("drink[guoz]");
					editRes=MeetService.me.update(xh,room,topic,starttime,finishtime,zcr,chr,prechr,bz,ydr,device,isprivate,isremind,isdrink,url,meis,naik,kaib,nat,xiangc,zheng,jiaot,mok,guoz);
				}			
				setAttr("res", editRes);
			}	
		}
		renderJson();
	}

	public void deletemeet() {
		boolean deleteRes = false;
		String xh = getPara("xh");
		//String url = "http://" + getRequest().getServerName() + "/oathSub?" + "meet/meetinfo";
		String url = "http://" + getRequest().getServerName() + baseurl;
		deleteRes=MeetService.me.deletemeet(xh,url);
		setAttr("res", deleteRes);
		renderJson();
	}

	// 判断是否已经有记录
	public void ordermeeting() {
		String date = getPara("date");// 得到当前日期
		renderJsp("meeting.jsp");
	}

	// 1.5 confrim-->这个东西是做到验证机制(拦截器中还是方法中好) -->拦截器中通用。
	public void grant() {
		getPara();
	}

	// extra funtion
	public Boolean confirmUser(String curuser, String fqruser) {
		boolean veruser = false;
		if (curuser.equals(fqruser))
			veruser = true;
		return veruser;
	}

	// unit test
	public static void main(String[] args) {
		try {
			System.out.println(MeetController.class.newInstance().confirmUser("he", "he"));
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 4. new tab
	 */

}
